package org.budo.mybatis.dao.statement.builder.creator;

import org.apache.ibatis.session.Configuration;
import org.budo.mybatis.dao.statement.builder.impl.DefaultStatementBuilder;

/**
 * @author lmw
 */
public class FindByColumnsStatementCreator extends AbstractStatementCreator {
    public FindByColumnsStatementCreator(Configuration configuration, DefaultStatementBuilder statementBuilder) {
        super(configuration, statementBuilder);
    }

    protected String getStatementId() {
        return STATEMENT_FIND_BY_COLUMNS;
    }

    protected String createStatement(Class<?> entityType) {
        String tableName = this.getStatementBuilder().getEntityConfiger().getTableName(entityType);
        String resultMapOrResultType = this.getStatementBuilder().resultMapOrResultType(this.getConfiguration(), entityType, null);

        String statementXml = _4 + "<select id=\"" + this.getStatementId() + "\" " + resultMapOrResultType + ">\n"//
                + _8 + "SELECT * FROM " + tableName + " WHERE \n" //
                + _12 + "<foreach item=\"key\" collection=\"columnsAndValues.keys\" separator=\" AND \">\n" //
                + _16 + "${key} = #{columnsAndValues.${key}}\n" //
                + _12 + "</foreach>" + "\n" //
                + _8 + " LIMIT 1 \n" //
                + _4 + "</select>"; //
        return statementXml;
    }
}